package co.fitstart.weapp.api.web.controller.coach;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.result.HttpResult;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.principal.Principal;
import co.fitstart.weapp.api.service.CoachService;
import co.fitstart.weapp.api.service.CourseRatingService;

/**
 * @ClassName: CoachCourseRatingController
 * @author: cuteHU
 * @date: 2018年12月13日
 */
@RestController
@RequestMapping("/coach/course-grades")
public class CoachCourseRatingController {

    private static final Logger logger = LoggerFactory.getLogger(CoachScheduleController.class);

    @Autowired
    private CoachService coachService;

    @Autowired
    private CourseRatingService courseRatingService;
    
    /**
     * @Description: 进入教练课时评分列表(默认来到未评分)
     * @param: @param mobile
     * @param: @param rated
     * @param: @return
     * @return: HttpResult
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "mobile")
    public HttpResult listCourseGrade(@RequestParam("mobile") String mobile,
                                      @RequestParam(value = "rated", required = false, defaultValue = "false") boolean rated,
                                      @RequestParam(value = "begin", required = false, defaultValue = "0") int begin,
                                      @RequestParam(value = "length", required = false, defaultValue = "10") int length) {
        HttpResult httpResult = new HttpResult();
        Principal coach = coachService.load(mobile);

        try {
            
            List<CourseSchedule> courseSchedules = courseRatingService.list(coach, rated, begin, length);
            
            httpResult.success().message("获取数据成功")
                      .data("courseSchedules", courseSchedules);
            
        } catch (Exception e) {
            logger.error("#CoachCourseRatingController# listCourseGrade() error", e);
            httpResult.fail("500").message("获取数据失败");
        }
        return httpResult;
    }
    
    /**
     * @Description: 学员评分详情信息页
     * @param:  @param mobile
     * @param:  @param courseSchduleId
     * @param:  @return 
     * @return:  HttpResult
     */
    @RequestMapping(value="/{courseSchduleId}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,params="mobile")
    public HttpResult ratingDetail(@RequestParam("mobile") String mobile,
                                   @PathVariable("courseSchduleId") Long courseSchduleId) {
        
        HttpResult httpResult=new HttpResult();
        try {
            CourseSchedule courseSchedule=courseRatingService.load(courseSchduleId);
            httpResult.success().message("获取数据成功")
                      .data("courseSchedule", courseSchedule)
                      .data("student", courseSchedule.getStudent())
                      .data("site", courseSchedule.getSite());
        } catch (Exception e) {
            logger.error("#CoachCourseRatingController#", e);
            httpResult.fail("500").message("获取数据失败");
        }
        return httpResult;
    }
    
    /**
     * @Description: 教练评分
     * @param:  @param mobile
     * @param:  @param courseSchduleId
     * @param:  @param grade
     * @param:  @return 
     * @return:  HttpResult
     */
    @RequestMapping(value="/{courseSchduleId}/rating",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,params="mobile")
    public HttpResult rateCourse(@RequestParam("mobile")String mobile,
            @PathVariable("courseSchduleId") Long courseSchduleId,
            @RequestParam("grade") int grade) {
        HttpResult httpResult=new HttpResult();
        CourseSchedule courseSchedule=courseRatingService.load(courseSchduleId);
        
        try {
            courseRatingService.rate(courseSchedule,grade);
            httpResult.success().message("已评分");
        } catch (Exception e) {
            logger.error("#CoachProfileController#", e);
            httpResult.fail("500").message("评分失败");
        }
        return httpResult;    
    }

}
